/**
 * @file app_main.c
 * @brief 
 * @author yejing (yejh@gongniu.cn)
 * @version 1.0
 * @date 2024-05-15
 * 
 * Copyright 2024-2025 GongNiu Inc. All Rights Reserved.
 * 
 */

#include <stdio.h>

#include "bull_hal_sys_thread.h"
#include "bull_hal_sys_system.h"
#include "bull_hal_drv_wifi.h"
#include "bull_hal_sys_memory.h"
#include "esp_log.h"
static const char* WIFI_TAG = "wifi_drv";

/**
 * @brief 
 * 
 * @param param 
 */
static void app_init_thread(void* param)
{

    bull_hal_drv_wifi_init( NULL );

    // wifi作为sta模式连接路由器
    bull_hal_drv_wifi_station_connect((const char *)"GN-XIU",(const char *)"88888888");

    int8_t wifi_rssi = 0 ;

    AP_IF_S  *ap_ary = NULL;

    while ( 1 ){

        ESP_LOGE( WIFI_TAG, "free heap:%ld", bull_hal_sys_get_free_heap_size() );

        bull_hal_drv_wifi_station_get_conn_ap_rssi( &wifi_rssi );
        ESP_LOGI( WIFI_TAG, "rssi:%d", wifi_rssi );

        NW_MAC_S mac  = { 0 };
        bull_hal_drv_wifi_get_mac( WF_STATION, &mac );
        // ESP_LOGI( WIFI_TAG,"read wifi mac:" MACSTR "\r\n", MAC2STR(mac.mac));

        NW_IP_S ip = { 0 };
        bull_hal_drv_wifi_get_ip( WF_STATION, &ip );
        ESP_LOGI( WIFI_TAG,"ip:%s gw:%s mask:%s", ip.ip, ip.gw, ip.mask );

        UCHAR_T primary_chan;
        bull_hal_drv_wifi_get_cur_channel( &primary_chan );
        ESP_LOGI( WIFI_TAG,"primary_chan:%d", primary_chan );

        uint16_t  ap_ary_num = 0 ;
        bull_hal_drv_wifi_all_ap_scan( &ap_ary, &ap_ary_num );

        for (size_t i = 0; i < ap_ary_num; i++){

            ESP_LOGI( WIFI_TAG,"ssid:%s rssi:%d", ap_ary[i].ssid, ap_ary[i].rssi  );
        }

        if ( ap_ary != NULL )
        {
            ESP_LOGI( WIFI_TAG,"free ap_ary" );

            bull_hal_sys_memory_free( ap_ary );

            ap_ary = NULL;
        }

        ESP_LOGE( WIFI_TAG, "free heap:%ld", bull_hal_sys_get_free_heap_size() );

        ESP_LOGI( WIFI_TAG,"——————————————————————————————————" );
        bull_hal_sys_sleep( 1000 );
    }
}

/**
 * @brief 
 * 
 */
static void app_init_entry(void)
{

    bull_hal_sys_thread_create( app_init_thread,
    "app_init_thread",
    4*4096 ,
    NULL,
    10,
    NULL);
}

/**
 * @brief 
 * 
 */
void app_main(void)
{

    app_init_entry();
}
